package com.sanhuo.security;

import com.sanhuo.security.filter.HomeFilter;
import com.sanhuo.security.filter.LoginFilter;
import com.sanhuo.security.filter.TokenFilter;
import com.sanhuo.security.filter.UrlFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;


@Configuration
@EnableWebMvc
public class WebSecurityConfit implements WebMvcConfigurer {
    private final String RESOURCE = "/static/**";
    private final String LOGIN = "/login";
    private final String LOGINPAGE = "/";
    private final String VCODE = "/verifyCode";
    private final String HOME = "/home";
    private final String ASSEMBLY = "/page/**";
    @Autowired
    private LoginFilter loginFilter;
    @Autowired
    private TokenFilter tokenFilter;
    @Autowired
    private UrlFilter urlFilter;
    @Autowired
    private HomeFilter homeFilter;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        /* 登录拦截器 只拦截登录 */
        LoginConfigInit(registry);
        /* token拦截器 拦截所有,除去静态资源和登录界面 */
        tokenConfigInit(registry);
        homeConfigInit(registry);


    }

    /**
     * 跨域配置
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {

        corsConfigInit(registry);
    }

    /**
     * jsp配置
     *
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        jspConfigInit(registry);

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        resourceConfigInit(registry);
    }

    private void homeConfigInit(InterceptorRegistry registry) {
        registry.addInterceptor(homeFilter)
                .addPathPatterns(HOME);
    }

    /**
     * 登录拦截器
     */
    private void LoginConfigInit(InterceptorRegistry registry) {
        registry.addInterceptor(loginFilter)
                .addPathPatterns(LOGIN);
    }

    /**
     * token拦截器
     */
    private void tokenConfigInit(InterceptorRegistry registry) {
        registry.addInterceptor(tokenFilter)
                .addPathPatterns("/**")
                .excludePathPatterns("")
                .excludePathPatterns(ASSEMBLY, HOME)//网页组件
                .excludePathPatterns(RESOURCE)//静态资源
                .excludePathPatterns(LOGIN, LOGINPAGE)//登录相关
                .excludePathPatterns(VCODE);//验证码
    }

    private void corsConfigInit(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                // 设置允许跨域请求的域名
                // 是否允许证书 不再默认开启
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("*")
                // 跨域允许时间
                .maxAge(3600);
    }

    private void jspConfigInit(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/view/", "");
    }

    private void resourceConfigInit(ResourceHandlerRegistry registry) {
        //指定了静态资源文件的位置
        registry.addResourceHandler("/static/**")
                .addResourceLocations("/WEB-INF/static/");
    }
}
